//
//  MOTabBar.m
//  01,我的微博 (环境配置)
//
//  Created by user on 16/3/4.
//  Copyright © 2016年 son. All rights reserved.
//

#import "MOTabBar.h"
#import "MOTabBarButton.h"


@interface MOTabBar()

//创建一个控件 (把这个控件添加到tabbarItem上)
@property (nonatomic,weak) UIButton *plusButton;

@property (nonatomic,strong) NSMutableArray *buttons;

//点击tabBarButton
@property (nonatomic,weak) UIButton *selectedButton;


@end

@implementation MOTabBar

//懒加载

- (NSMutableArray *)buttons{
    if(!_buttons) {
        
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

- (UIButton *)plusButton{
    
    if(!_plusButton) {
        
        //创建button
        //UIButton *btn = [[UIButton alloc] init];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"添加"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"添加"] forState:UIControlStateHighlighted];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"添加"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"添加"] forState:UIControlStateHighlighted];
        
        //默认按钮的尺寸跟背景图片一样大
        //sizeToFit:默认会根据按钮的背景图片或者image和文字计算出按钮的最合适的尺寸
        [btn sizeToFit];
        
        //监听按钮的点击
        [btn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
        
        _plusButton = btn;
        
        
        //把按钮添加到Tabbar上
        [self addSubview:_plusButton];
    }
  
    return _plusButton;
}

#pragma mark - 点击加号按钮的时候调用
- (void)plusClick{
    
    //modal出控制器
    if ([self.delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
        
        [self.delegate tabBarDidClickPlusButton:self];
    }
    
}

//重写属性的set方法来添加按钮
- (void)setItems:(NSArray *)items{
    _items = items;
    
    //遍历模型数组,创建对应tabBarButton
    for(UITabBarItem *item in _items) {
        
        MOTabBarButton *btn =[MOTabBarButton buttonWithType:UIButtonTypeCustom];
        //给按钮赋值模型,按钮的内容由模型对应决定
        btn.item = item;
        
        btn.tag = self.buttons.count;
        
        //监听按钮点击事件
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
        //选中第0个
        if(btn.tag == 0) {
            
            [self btnClick:btn];
        }
        
        [self addSubview:btn];
        
        //把按钮添加到 按钮数组中
        [self.buttons addObject:btn];
        
    }
    
}

//点击tabBarButton调用
- (void)btnClick:(UIButton *)button{
    
    //三部曲 (点击其他图片的时候本身取消点击状态)
    self.selectedButton.selected = NO;
    button.selected = YES;
    self.selectedButton = button;
    
    //通知tabBarVc切换控制器
    if([self.delegate respondsToSelector:@selector(tabBar:didClickButton:)]){
        [self.delegate tabBar:self didClickButton:button.tag];
    }
    
}



//self.item UITabBarItem模型,有多少个子控制器就有多少个UITabBarItem模型
//调整子控件的位置
- (void)layoutSubviews{
    [super layoutSubviews];
    
   // NSLog(@"--");
    //整个tabbar的大小
    CGFloat w = self.bounds.size.width;
    CGFloat h = self.bounds.size.height;
    
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = w / (self.items.count + 1);//因为要添加一个按钮 所以要加1
    CGFloat btnH = h;
    
    int i = 0;
    
    //调整系统自带的tabBar上得按钮位置
    for(UIView *tabBarButton in self.buttons) {
    
            if(i == 2){ //当添加到第2个的时候让下一个控件从第4个位置开始添加按钮,第三个位置要添加一个按钮
                i = 3;
            }
            btnX = i * btnW;
            tabBarButton.frame = CGRectMake(btnX, btnY, btnW, btnH);
            i++;
        
    }
    
    //设置 添加按钮  的位置
    self.plusButton.center = CGPointMake(w * 0.5, h * 0.5);//中间位置
    
}

@end
